package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.ProductOrder;
import com.eralink.unionuhfreader.bean.ProductOrderDetailDO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProductOrderAdapter extends RecyclerView.Adapter<ProductOrderAdapter.OrderViewHold>{
    private List<ProductOrder> orders;
    private Context mContext;

    public  ProductOrderAdapter(Context c,List<ProductOrder> o){
        this.orders=o;
        this.mContext=c;
    }

    @NonNull
    @Override
    public OrderViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_order, parent, false);
        //将view传递给我们自定义的ViewHolder
        OrderViewHold holder=new OrderViewHold(view);
        //返回这个MyHolder实体
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHold holder, int position) {
        holder.order_id.setText(orders.get(position).getId());
        for (ProductOrderDetailDO detailDO : orders.get(position).getDetailDOS()) {
            TextView textView=new TextView(mContext);
            textView.setText(detailDO.getProductName()+"--"+detailDO.getRealPayPrice().toString());
            holder.order_detail_layout.addView(textView);
        }
        holder.yf_money.setText("应付总额:"+orders.get(position).getTotalAmount().toString());
        holder.real_money.setText("实际付款总额:"+orders.get(position).getRealTotalAmount().toString());
        holder.cost_price.setText("总成本:"+orders.get(position).getTotalCostAmount().toString());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        holder.create_time.setText(simpleDateFormat.format(orders.get(position).getCreateTime()));
        int paystatus=orders.get(position).getPayStatus();
        String status="待支付";
        if (paystatus==1){
            status="已支付";
        }else  if (paystatus==2){
            status="已取消";
        }else  if (paystatus==3){
            status="退款中";
        }else  if (paystatus==4){
            status="退款审核中";
        }else  if (paystatus==5){
            status="已退款";
        }else  if (paystatus==6){
            status="已部分退款";
        }
        holder.pay_status.setText(status);
        holder.storeName.setText(orders.get(position).getStoreName());
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    class OrderViewHold extends RecyclerView.ViewHolder{
        private TextView order_id;
        private LinearLayout order_detail_layout;
        private TextView yf_money,real_money,cost_price;
        private TextView pay_type,pay_status,create_time,storeName;
        public OrderViewHold(View convertView) {
            super(convertView);
            order_id = (TextView) convertView.findViewById(R.id.order_id);
            order_detail_layout = (LinearLayout) convertView.findViewById(R.id.order_detail_layout);
            yf_money = (TextView) convertView.findViewById(R.id.yf_money);
            real_money= (TextView) convertView.findViewById(R.id.real_money);
            cost_price= (TextView) convertView.findViewById(R.id.cost_price);
            pay_type= (TextView) convertView.findViewById(R.id.pay_type);
            pay_status= (TextView) convertView.findViewById(R.id.pay_status);
            create_time= (TextView) convertView.findViewById(R.id.create_time);
            storeName=(TextView) convertView.findViewById(R.id.storeName);
        }
    }
}
